/**
 * 
 */
package com.hand.cloud.b2b.taobao.impl;

import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.servicelayer.model.ModelService;

import org.springframework.beans.factory.annotation.Autowired;

import com.hand.cloud.b2b.daos.EPProductDAO;
import com.hand.cloud.b2b.daos.ProductDAO;
import com.hand.cloud.b2b.daos.TBProductDAO;
import com.hand.cloud.b2b.model.EPProductModel;
import com.hand.cloud.b2b.model.TB_ProductModel;
import com.hand.cloud.b2b.taobao.DefaultProperties;
import com.hand.cloud.b2b.taobao.ItemOperationService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.ItemAddRequest;
import com.taobao.api.request.ItemDeleteRequest;
import com.taobao.api.request.ItemUpdateDelistingRequest;
import com.taobao.api.request.ItemUpdateListingRequest;
import com.taobao.api.request.ItemUpdateRequest;
import com.taobao.api.response.ItemAddResponse;
import com.taobao.api.response.ItemDeleteResponse;
import com.taobao.api.response.ItemUpdateDelistingResponse;
import com.taobao.api.response.ItemUpdateListingResponse;
import com.taobao.api.response.ItemUpdateResponse;


/**
 * @author StephenWang
 * @Time Aug 19, 2014 9:40:44 AM
 */
public class DefaultItemOperationService implements ItemOperationService
{

	@Autowired
	private ProductDAO productDAO;

	@Autowired
	private EPProductDAO epProductDAO;

	@Autowired
	private TBProductDAO tbProductDAO;

	@Autowired
	private ModelService modelService;

	/**
	 * 将Hybris商品同步到淘宝
	 */
	@Override
	public void createItem()
	{

		final ProductModel product = this.productDAO.getProductByPK("8796137586689");

		final EPProductModel epProduct = this.epProductDAO.createEPProduct(product);

		final TB_ProductModel tbProduct = this.tbProductDAO.createTBProduct(epProduct);

		final TaobaoClient client = new DefaultTaobaoClient(DefaultProperties.serverUrl, DefaultProperties.appKey,
				DefaultProperties.appSecret);

		final ItemAddRequest req = new ItemAddRequest();

		req.setPrice(String.valueOf(tbProduct.getPrice()));//价格
		req.setNum(tbProduct.getNum());//数量
		req.setType(tbProduct.getType());//一口价，非拍卖
		req.setStuffStatus(tbProduct.getStuff_status());//全新，非二手
		req.setTitle(tbProduct.getTitle());
		req.setDesc(tbProduct.getDesc());
		req.setLocationState(tbProduct.getT_location_state());
		req.setLocationCity(tbProduct.getT_location_city());
		req.setCid(tbProduct.getCid());//电子凭证，商品子类目
		req.setOuterId(tbProduct.getOuter_id());//外部系统的主键

		try
		{
			final ItemAddResponse response = client.execute(req, DefaultProperties.sessionKey);

			System.out.println(response.getBody());
			final Long numIid = response.getItem().getNumIid();
			tbProduct.setNumIid(numIid);
			epProduct.setNumIid(numIid);
			product.setNumIid(numIid);


			tbProduct.setNum(response.getItem().getNumIid());

			// tbProductDAO.updateNumIid(String tbProductPK, Long numIid);
		}
		catch (final ApiException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 删除商品
	 * 
	 * @param numIid
	 */
	public void deleteItem(final long numIid)
	{
		final TaobaoClient client = new DefaultTaobaoClient(DefaultProperties.serverUrl, DefaultProperties.appKey,
				DefaultProperties.appSecret);

		final ItemDeleteRequest req = new ItemDeleteRequest();
		req.setNumIid(numIid);

		try
		{
			final ItemDeleteResponse response = client.execute(req, DefaultProperties.sessionKey);
			System.out.println(response.getBody());
			System.out.println(response.getMsg());
		}
		catch (final ApiException e)
		{
			e.printStackTrace();
		}
	}



	/**
	 * 将Hybris中的商品信息更新到淘宝上
	 */
	@Override
	public void updateItem()
	{
		// YTODO Auto-generated method stub
		final ProductModel product = this.productDAO.getProductByPK("8796137586689");

		final EPProductModel epProduct = this.epProductDAO.updateEPProduct(product);

		final TB_ProductModel tbProduct = this.tbProductDAO.updateTBProduct(epProduct);

		final TaobaoClient client = new DefaultTaobaoClient(DefaultProperties.serverUrl, DefaultProperties.appKey,
				DefaultProperties.appSecret);
		final ItemUpdateRequest req = new ItemUpdateRequest();

		req.setNum(tbProduct.getNum());//数量
		req.setPrice(String.valueOf(tbProduct.getPrice()));//价格
		req.setStuffStatus(tbProduct.getStuff_status());//全新，非二手
		req.setTitle(tbProduct.getTitle());
		req.setDesc(tbProduct.getDesc());
		req.setLocationState(tbProduct.getT_location_state());
		req.setLocationCity(tbProduct.getT_location_city());
		req.setCid(tbProduct.getCid());//电子凭证，商品子类目
		try
		{
			final ItemUpdateResponse response = client.execute(req, DefaultProperties.sessionKey);

			System.out.println(response.getBody());
		}
		catch (final ApiException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * 商品上架
	 */
	@Override
	public void updateItemDelisting(final long numIid)
	{
		// YTODO Auto-generated method stub
		final TaobaoClient client = new DefaultTaobaoClient(DefaultProperties.serverUrl, DefaultProperties.appKey,
				DefaultProperties.appSecret);
		final ItemUpdateDelistingRequest req = new ItemUpdateDelistingRequest();
		req.setNumIid(numIid);
		try
		{
			final ItemUpdateDelistingResponse response = client.execute(req, DefaultProperties.sessionKey);
			System.out.println(response.getBody());
			System.out.println(response.getMsg());
		}
		catch (final ApiException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * 商品下架
	 */
	@Override
	public void updateItemListing(final long numIid, final long num)
	{
		// YTODO Auto-generated method stub
		final TaobaoClient client = new DefaultTaobaoClient(DefaultProperties.serverUrl, DefaultProperties.appKey,
				DefaultProperties.appSecret);
		final ItemUpdateListingRequest req = new ItemUpdateListingRequest();
		req.setNumIid(numIid);
		req.setNum(num);
		try
		{
			final ItemUpdateListingResponse response = client.execute(req, DefaultProperties.sessionKey);
			System.out.println(response.getBody());
		}
		catch (final ApiException e)
		{
			e.printStackTrace();
		}
	}

	public static void main(final String[] args)
	{
		final DefaultItemOperationService itemOperation = new DefaultItemOperationService();
		itemOperation.deleteItem(23242L);
		//itemOperation.createItem();
	}

}
